package com.sgave.blog.controller;

import com.sgave.blog.model.Label;
import com.sgave.blog.model.Result;
import com.sgave.blog.service.LabelService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author fly
 * @date 2024/1/10 11:51
 */
@Api(tags = "标签管理")
@RestController
@RequestMapping("/blog/label")
@CrossOrigin
public class LabelController {

    @Resource
    private LabelService labelService;

    @ApiOperation(value = "新增标签")
    @PostMapping("add")
    public Result addLabel(@RequestBody Label label){
        int result = labelService.addLabel(label);
        if (result > 0) {
            return Result.success("新增成功", label);
        } else {
            return Result.error("新增失败");
        }
    }

    @ApiOperation(value = "查询所有标签")
    @GetMapping("selectAll")
    public Result selectAll(){
        List<Label> labelList = labelService.selectAll();
        return Result.success(labelList);
    }

    @ApiOperation(value = "根据id删除标签")
    @DeleteMapping("/{id}")
    public Result deleteById(@PathVariable Integer id){
        int result = labelService.deleteById(id);
        if (result > 0) {
            return Result.success("删除成功");
        } else {
            return Result.error("删除失败，标签已使用");
        }

    }

}
